﻿<%@ WebHandler Language="C#"  Class="Smy.Web.tools.browser_redirect" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Smy.Web.tools
{
  /// <summary>
  /// browser_redirect 的摘要说明
  /// </summary>
  public class browser_redirect : IHttpHandler
  {

    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/javascript";
      context.Response.Write("function getMCookie(objName) {");
      context.Response.Write("    var arrStr = document.cookie.split(\"; \");");
      context.Response.Write("    for (var i = 0; i < arrStr.length; i++) {");
      context.Response.Write("        var temp = arrStr[i].split(\"=\");");
      context.Response.Write("        if (temp[0] == objName) {");
      context.Response.Write("            return unescape(temp[1]);");
      context.Response.Write("        }");
      context.Response.Write("    }");
      context.Response.Write("    return \"\";");
      context.Response.Write("}");
      context.Response.Write("");
      context.Response.Write("function browserRedirect() {");
      context.Response.Write("    var sUserAgent = navigator.userAgent.toLowerCase();");
      context.Response.Write("    var bIsIpad = sUserAgent.match(/ipad/i) == \"ipad\";");
      context.Response.Write("    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == \"iphone os\";");
      context.Response.Write("    var bIsMidp = sUserAgent.match(/midp/i) == \"midp\";");
      context.Response.Write("    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == \"rv:1.2.3.4\";");
      context.Response.Write("    var bIsUc = sUserAgent.match(/ucweb/i) == \"ucweb\";");
      context.Response.Write("    var bIsAndroid = sUserAgent.match(/android/i) == \"android\";");
      context.Response.Write("    var bIsCE = sUserAgent.match(/windows ce/i) == \"windows ce\";");
      context.Response.Write("    var bIsWM = sUserAgent.match(/windows mobile/i) == \"windows mobile\";");
      context.Response.Write("    if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {");
      context.Response.Write("        location.href = \'/m/index.html\';");
      context.Response.Write("    }");
      context.Response.Write("}");
      context.Response.Write("var pageurl = window.location.search;");
      context.Response.Write("if (pageurl == \'?m2w\') {");
      context.Response.Write("    document.cookie = \"m2wcookie=\" + escape(\"1\");");
      context.Response.Write("}");
      context.Response.Write("if (getMCookie(\"m2wcookie\") != \"1\") browserRedirect();");
    }

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}